package com.datawale.vh.demo.work.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.TreeMap;

/**
 * @author huangtao
 * @time 2023/6/13
 * @description
 */
@Slf4j
public class FileUtil {

    public static boolean uploadToServer(MultipartFile multipartFile, String uploadPath, String uploadFileName){
        //构建文件对象
        File file = new File(uploadPath);
        if (!file.exists()){
            boolean mkdirs = file.mkdirs();
            if (!mkdirs){
                log.error("创建文件夹异常");
                return false;
            }
        }

        InputStream ins = null;
        FileOutputStream outs = null;
        try {
            //获取文件输入流
            ins = multipartFile.getInputStream();
            //构建文件输出流
            outs = new FileOutputStream(uploadPath + uploadFileName);
            int len;
            byte[] bytes = new byte[1024];
            //读取一个bytes的文件内容
            while ((len = ins.read(bytes)) != -1){
                outs.write(bytes,0,len);
            }
            outs.close();
            log.info("上传成功：{}",uploadPath+uploadFileName);
            return true;
        }catch (Exception e){
            log.error("文件上传异常");
            e.printStackTrace();
        }finally {
            try {
                if (outs != null){
                    outs.close();
                }
                if (ins != null){
                    ins.close();
                }
            }catch (IOException e){
                log.error("关闭流异常");
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean newUploadToServer(MultipartFile multipartFile,String uploadPath,String uploadFileName){
        File file = new File(uploadPath);
        if (!file.exists()){
            boolean mkdirs = file.mkdirs();
            if (!mkdirs){
                log.error("创建文件夹异常");
                return false;
            }
        }
        try {
            InputStream inputStream = multipartFile.getInputStream();
            FileOutputStream outputStream = new FileOutputStream(uploadPath + uploadFileName);
            int copy = FileCopyUtils.copy(inputStream,outputStream);
            log.info("上传成功，文件大小：{}",copy);
            return true;
        }catch (IOException e){
            log.error("文件上传异常",e);
            e.printStackTrace();
        }
        return false;
    }

    //远端文件下载到本地服务器
    public static boolean downloadToServer(String downloadUrl,String downloadPath,String downloadFileName){
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        boolean flag = false;
        try {
            URL url = new URL(downloadUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            bis = new BufferedInputStream(connection.getInputStream());
            File file = new File(downloadPath);

            String filePathName = downloadPath+File.separator+downloadFileName;
            byte[] buf = new byte[1024];
            int size;
            fos = new FileOutputStream(filePathName);
            while ((size = bis.read(buf)) != -1){
                fos.write(buf,0,size);
            }
            flag = true;
        }catch (Exception e){
            log.error("文件下载异常",e);
        }finally {
            try {
                if (bis != null){
                    bis.close();
                }
                if (fos != null){
                    fos.close();
                }
            }catch (IOException e){
                log.error("关闭流异常");
                e.printStackTrace();
            }
        }
        return flag;
    }
}
